package constellation.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.R;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import constellation.Activity.StartInfoActivity;
import constellation.Activity.constellationImageActivity;
import constellation.adapter.StartItemGridviewAdapter;
import constellation.adapter.StartPagerAdapter;
import constellation.util.AssetsUtil;
import constellation.util.StartInfo;

/**
 * A simple {@link Fragment} subclass.
 */
public class starFragment extends Fragment {

    private ViewPager startVp;
    private LinearLayout startlayout;
    private GridView startGridview;
    View view;
    Context context;
    StartItemGridviewAdapter adapter;
    int []images={R.mipmap.pic_guanggao,R.mipmap.pic_star};
    List<ImageView>imageViewList;
    List<ImageView>iconList;
    StartPagerAdapter pagerAdapter;
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {

            if (msg.what==1){
                int currentItem = startVp.getCurrentItem();
                if (currentItem ==imageViewList.size()-1) {
                    startVp.setCurrentItem(0);
                }else {
                    currentItem++;
                    startVp.setCurrentItem(currentItem);
                }
                handler.sendEmptyMessageDelayed(1,2000);
            }

        }
    };
    public starFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_star, container, false);
        context=getContext();
        initView();
        initData();
        initPage();
        setVPListener();
        handler.sendEmptyMessageDelayed(1,2000);
        setGridViewListener();
        return view;
    }
    List<StartInfo.StarinfoBean> list;
public void initData(){
   //将json字符串转换成创建类的对象
    Bundle bundle = getArguments();
    StartInfo starInfo = (StartInfo) bundle.getSerializable("info");
    list = starInfo.getStarinfo();  //数据源
    adapter=new StartItemGridviewAdapter(context,list);
    startGridview.setAdapter(adapter);
}
    private void initView( ) {
        startVp = (ViewPager) view.findViewById(R.id.start_vp);
        startlayout = (LinearLayout) view.findViewById(R.id.startlayout);;
        startGridview = (GridView) view.findViewById(R.id.start_gridview);
    }
    public void initPage(){
        imageViewList=new ArrayList<>();
        iconList=new ArrayList<>();
        for (int i = 0; i < images.length; i++) {
            ImageView iv=new ImageView(context);
            iv.setImageResource(images[i]);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
            iv.setLayoutParams(lp);
            imageViewList.add(iv);
            ImageView piv=new ImageView(context);
            piv.setImageResource(R.drawable.point_normal);
            LinearLayout.LayoutParams plp=new LinearLayout.LayoutParams(30,30);
            plp.setMargins(20,20,0,0);
            piv.setLayoutParams(plp);
            startlayout.addView(piv);
            iconList.add(piv);
        }
        iconList.get(0).setImageResource(R.drawable.point_select);
        pagerAdapter=new StartPagerAdapter(context,imageViewList);
        startVp.setAdapter(pagerAdapter);
    }
    public void  setVPListener(){
        startVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < imageViewList.size(); i++) {
                    iconList.get(i).setImageResource(R.drawable.point_normal);
                }
                iconList.get(position).setImageResource(R.drawable.point_select);

            }


            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    public void setGridViewListener(){
        startGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(context, StartInfoActivity.class).putExtra("details",list.get(position)));
            }
        });
    }
}
